package neu.soft.costumer;
import lombok.extern.slf4j.Slf4j;
import neu.soft.entity.DistributionTaskOrder;
import neu.soft.exception.AppException;
import neu.soft.exception.AppExceptionCodeMsg;
import neu.soft.service.TaskOrderService;
import neu.soft.vo.MsgBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ReceiveService
{
    @Autowired
    private TaskOrderService taskOrderService;

    @StreamListener("distribution-channel")
    public void receiveInput1(@Payload MsgBean receiveMsg){
        List<Map<String, Object>> distributionTaskOrderList = (List<Map<String, Object>>) receiveMsg.getData();
        System.out.println("input1 receive: " + distributionTaskOrderList);


        try {
            taskOrderService.storeTaskOrders(distributionTaskOrderList);

        } catch (Exception e) {
            log.info(e.getMessage());
            throw new AppException(AppExceptionCodeMsg.DZYJ_NGDZ);
        }finally {
            List<String> strings = distributionTaskOrderList.stream().map(s->(String)s.get("distributionTaskOrderId")).collect(Collectors.toList());
            log.info(strings.toString());
        }

    }


}
